package com.greendizer.api.test.snippets;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.junit.Test;

import com.greendizer.api.client.SellerClient;
import com.greendizer.api.dal.Collection;
import com.greendizer.api.resource.seller.Email;
import com.greendizer.api.resource.seller.Invoice;
import com.greendizer.api.resource.seller.Seller;


public class FourthSnippet {

  @Test
  public void SnippetTest() {
    Seller seller = new SellerClient("oauth_token").getUser();
    Email email = seller.getEmails().getById("example@domain.com");

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DATE, 1);
    String currentMonth = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    String query = String.format("canceled==false|date>>%s", currentMonth);

    Collection<Invoice> invoices = email.getInvoices().search(query);
    while (invoices.getResources().size() < invoices.size()) {
      invoices.populate(invoices.getResources().size(), 200);
    }

    Double total = 0d;
    for (Invoice invoice : invoices) {
      if (invoice.getCurrency().equals("USD")) {
        total += invoice.getTotal();
      }
    }

    System.out.println(String.format("Total for %s: $%f", currentMonth, total));
  }
}
